package edu.yzu.testspringboot002.utils;

import lombok.*;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author yulang
 */
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;


    @Getter
    @Setter
    private T data;

    public static <T> ResponseResult<T> ok() {
        return restResult(null, HttpStatus.OK.value(), "操作成功！");
    }

    public static <T> ResponseResult<T> ok(T data) {
        return restResult(data, HttpStatus.OK.value(), "操作成功！");
    }

    public static <T> ResponseResult<T> ok(T data, String msg) {
        return restResult(data, HttpStatus.OK.value(), msg);
    }

    public static <T> ResponseResult<T> failed() {
        return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败！");
    }

    public static <T> ResponseResult<T> failed(String msg) {
        return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static <T> ResponseResult<T> failed(T data) {
        return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败！");
    }

    public static <T> ResponseResult<T> failed(T data, String msg) {
        return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static <T> ResponseResult<T> failed(Integer code, String msg) {
        return restResult(null, code == null ? HttpStatus.INTERNAL_SERVER_ERROR.value() : code, msg);
    }

    private static <T> ResponseResult<T> restResult(T data, int code, String msg) {
        ResponseResult<T> apiResult = new ResponseResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }


}
